

package com.ylbh.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.exception.RRException;
import com.ylbh.common.pojo.sys.dto.AddSupplierBillDTO;
import com.ylbh.common.pojo.sys.dto.SupplierBillDTO;
import com.ylbh.common.pojo.sys.dto.SysUserDTO;
import com.ylbh.common.pojo.sys.vo.SupplierBillVO;
import com.ylbh.common.pojo.sys.vo.SupplierListVO;
import com.ylbh.common.pojo.sys.vo.SysUserVO;
import com.ylbh.common.utils.*;
import com.ylbh.common.exception.RRException;
import com.ylbh.common.utils.Constant;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.Query;
import com.ylbh.modules.sys.dao.SysUserDao;
import com.ylbh.modules.sys.entity.SysUserEntity;
import com.ylbh.modules.sys.service.SysRoleService;
import com.ylbh.modules.sys.service.SysUserRoleService;
import com.ylbh.modules.sys.service.SysUserService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.renren
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysUserDao SysUserDao;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String username = (String)params.get("userName");
		Long createUserId = (Long)params.get("createUserId");

		IPage<SysUserEntity> page = this.page(
			new Query<SysUserEntity>().getPage(params),
			new QueryWrapper<SysUserEntity>()
				.like(StringUtils.isNotBlank(username),"username", username)
				.eq(createUserId != null,"create_user_id", createUserId)
		);

		return new PageUtils(page);
	}

	@Override
	public List<String> queryAllPerms(Long userId) {
		return baseMapper.queryAllPerms(userId);
	}

	@Override
	public List<Long> queryAllMenuId(Long userId) {
		return baseMapper.queryAllMenuId(userId);
	}

	@Override
	public SysUserEntity queryByUserName(String username) {
		return baseMapper.queryByUserName(username);
	}

	@Override
	@Transactional
	public void saveUser(SysUserEntity user) {
		user.setCreateTime(new Date());
		//sha256加密
		String salt = RandomStringUtils.randomAlphanumeric(20);
		user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
		user.setSalt(salt);
		this.save(user);

		//检查角色是否越权
		checkRole(user);

		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
	}

	@Override
	@Transactional
	public void update(SysUserEntity user) {
		if(StringUtils.isBlank(user.getPassword())){
			user.setPassword(null);
		}else{
			user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
		}
		this.updateById(user);

		//检查角色是否越权
		checkRole(user);

		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
	}

	@Override
	public void deleteBatch(Long[] userId) {
		this.removeByIds(Arrays.asList(userId));
	}

	@Override
	public boolean updatePassword(Long userId, String password, String newPassword) {
		SysUserEntity userEntity = new SysUserEntity();
		userEntity.setPassword(newPassword);
		return this.update(userEntity,
				new QueryWrapper<SysUserEntity>().eq("user_id", userId).eq("password", password));
	}

	@Override
	public void updateLastLoginInfoById(Long userId, String ipAddr) {
		SysUserDao.updateLastLoginInfoById(userId,ipAddr);
	}

	/**
	 * 检查角色是否越权
	 */
	private void checkRole(SysUserEntity user){
		if(user.getRoleIdList() == null || user.getRoleIdList().size() == 0){
			return;
		}
		//如果不是超级管理员，则需要判断用户的角色是否自己创建
		if(user.getCreateUserId() == Constant.SUPER_ADMIN){
			return ;
		}

		//查询用户创建的角色列表
		List<Long> roleIdList = sysRoleService.queryRoleIdList(user.getCreateUserId());

		//判断是否越权
		if(!roleIdList.containsAll(user.getRoleIdList())){
			throw new RRException("新增用户所选角色，不是本人创建");
		}
	}

	/**
	 * 修改供应商信息
	 * @param user
	 */
	@Override
	public void updateSupplierById(SysUserDTO user) {
		if (user.getOperatingQualifiedImgurl().size() > 0) {
			for (int i = 0; i < user.getOperatingQualifiedImgurl().size(); i++) {
				if (StringUtils.isEmpty(user.getImg())) {
					user.setImg(user.getOperatingQualifiedImgurl().get(i));
				} else {
					user.setImg("," + user.getOperatingQualifiedImgurl().get(i));
				}
			}
		}
		SysUserDao.updateSupplierById(user);
	}

	/**
	 * 添加供应商信息
	 * @param user
	 */
	@Override
	public void addSupplierById(SysUserVO user) {
		if (user.getOperatingQualifiedImgurl().size() > 0) {
			for (int i = 0; i < user.getOperatingQualifiedImgurl().size(); i++) {
				if (StringUtils.isEmpty(user.getImg())) {
					user.setImg(user.getOperatingQualifiedImgurl().get(i));
				} else {
					user.setImg("," + user.getOperatingQualifiedImgurl().get(i));
				}
			}
		}
		SysUserDao.insertSupplierById(user);
	}

	/**
	 * 查询供应商信息
	 * @param userId
	 * @return
	 */
	@Override
	public SysUserVO getUserDataById(Long userId) {
		return SysUserDao.selectUserDataById(userId);
	}

	/**
	 * 查询现有供应商
	 * @return
	 */
	@Override
	public List<SupplierListVO> querySupplierList() {
		return SysUserDao.selectSupplierList();
	}

	/**
	 * 供应商账单列表
	 * @param supplierBillDTO
	 * @return
	 */
	@Override
	public PageUtils querySupplierBillList(SupplierBillDTO supplierBillDTO) {
		Page<Integer> page = new Page<>(supplierBillDTO.getPage(), supplierBillDTO.getLimit());
		List<SupplierBillVO> list = SysUserDao.selectSupplierBillList(supplierBillDTO, page);
		return new PageUtils(new Page<SupplierBillVO>().setRecords(list).setTotal(page.getTotal()));
	}

	/**
	 * 新增供应商账单
	 * @param addSupplierBillDTO
	 */
	@Override
	public void addSupplierBill(AddSupplierBillDTO addSupplierBillDTO) {
		addSupplierBillDTO.setOperator(ShiroUtils.getUserId().toString());
		addSupplierBillDTO.setId(new IdWorker().nextId());
		addSupplierBillDTO.setCreateTime(new Date());
		SysUserDao.insertSupplierBill(addSupplierBillDTO);
	}

	/**
	 * 删除供应商账单
	 * @param ids
	 */
	@Override
	public void modifySupplierBillById(List<String> ids) {
		SysUserDao.updateSupplierBillById(ids);
	}

//	@Override
//	public PageUtils queryPage(Map<String, Object> params) {
//		String username = (String)params.get("username");
//		Long createUserId = (Long)params.get("createUserId");
//
//		IPage<SysUserEntity> page = this.page(
//				new Query<SysUserEntity>().getPage(params),
//				new QueryWrapper<SysUserEntity>()
//						.like(StringUtils.isNotBlank(username),"username", username)
//						.eq(createUserId != null,"create_user_id", createUserId)
//		);
//
//		return new PageUtils(page);
//	}
//
//	@Override
//	public List<String> queryAllPerms(Long userId) {
//		return baseMapper.queryAllPerms(userId);
//	}
//
//	@Override
//	public List<Long> queryAllMenuId(Long userId) {
//		return baseMapper.queryAllMenuId(userId);
//	}
//
//	@Override
//	public SysUserEntity queryByUserName(String username) {
//		return baseMapper.queryByUserName(username);
//	}
//
//	@Override
//	@Transactional
//	public void saveUser(SysUserEntity user) {
//		user.setCreateTime(new Date());
//		//sha256加密
//		String salt = RandomStringUtils.randomAlphanumeric(20);
//		user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
//		user.setSalt(salt);
//		this.save(user);
//
//		//检查角色是否越权
//		checkRole(user);
//
//		//保存用户与角色关系
//		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
//	}
//
//	@Override
//	@Transactional
//	public void update(SysUserEntity user) {
//		if(StringUtils.isBlank(user.getPassword())){
//			user.setPassword(null);
//		}else{
//			user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
//		}
//		this.updateById(user);
//
//		//检查角色是否越权
//		checkRole(user);
//
//		//保存用户与角色关系
//		sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
//	}
//
//	@Override
//	public void deleteBatch(Long[] userId) {
//		this.removeByIds(Arrays.asList(userId));
//	}
//
//	@Override
//	public boolean updatePassword(Long userId, String password, String newPassword) {
//		SysUserEntity userEntity = new SysUserEntity();
//		userEntity.setPassword(newPassword);
//		return this.update(userEntity,
//				new QueryWrapper<SysUserEntity>().eq("user_id", userId).eq("password", password));
//	}
//
//	@Override
//	public void updateLastLoginInfoById(Long userId, String ipAddr) {
//		SysUserDao.updateLastLoginInfoById(userId,ipAddr);
//	}
//
//	/**
//	 * 检查角色是否越权
//	 */
//	private void checkRole(SysUserEntity user){
//		if(user.getRoleIdList() == null || user.getRoleIdList().size() == 0){
//			return;
//		}
//		//如果不是超级管理员，则需要判断用户的角色是否自己创建
//		if(user.getCreateUserId() == Constant.SUPER_ADMIN){
//			return ;
//		}
//
//		//查询用户创建的角色列表
//		List<Long> roleIdList = sysRoleService.queryRoleIdList(user.getCreateUserId());
//
//		//判断是否越权
//		if(!roleIdList.containsAll(user.getRoleIdList())){
//			throw new RRException("新增用户所选角色，不是本人创建");
//		}
//	}

	/**
	 * 供应商管理列表
	 * @param pageDTO
	 * @return
	 */
	@Override
	public PageUtils getSupplierAdministrationList(PageDTO pageDTO) {
		Page<Integer> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
		List<SysUserEntity> list = SysUserDao.selectSupplierAdministrationList(pageDTO.getKeywords(), page);
		return new PageUtils(new Page<SysUserEntity>().setTotal(page.getTotal()).setRecords(list));
	}
}
